package com.wnn.demo.model.business.controller;

import com.github.pagehelper.PageInfo;
import com.wnn.demo.model.business.entity.ProductInfo;
import com.wnn.demo.model.business.service.ProductInfoService;
import com.wnn.demo.pojo.Result;
import com.wnn.demo.pojo.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(value = "商品信息 controller", tags = "商品信息操作接口")
@RequestMapping("/product/info")
@RestController
public class ProductInfoController {

    @Resource
    private ProductInfoService productInfoService;

    /**
     * 查询所有商品信息
     * @param page 页数
     * @param limit 每页显示条数
     * @return 返回统一结果集 Result
     */
    @ApiOperation(value = "查询所有商品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页数", required = false, paramType = "query", dataType = "Integer", defaultValue = "1"),
            @ApiImplicitParam(name = "limit", value = "每页显示条数", required = false, paramType = "query", dataType = "Integer", defaultValue = "10")
    })
    @GetMapping("/queryAll")
    public Result queryProduct(@RequestParam(value = "page", defaultValue = "1") Integer page,
                               @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        PageInfo<ProductInfo> pageInfo = productInfoService.queryProduct(page, limit);

        return new Result(ResultCode.SUCCESS, pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 添加一条商品信息
     * @param productInfo 要添加的商品信息，传入要添加的商品实体类
     * @return 返回统一结果集 Result
     */
    @ApiImplicitParam(name = "productInfo", value = "传入 ProductInfo 实体类", required = true, paramType = "body", dataTypeClass = ProductInfo.class)
    @ApiOperation(value = "新增一件商品信息")
    @PostMapping("/addProduct")
    public Result addProduct(@RequestBody ProductInfo productInfo){
        Boolean added = productInfoService.addProduct(productInfo);
        if (!added) {
            return new Result(ResultCode.FALSE);
        }
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 删除一条商品信息
     * @param proId  要删除的商品信息的 ID
     * @return 统一结果
     */
    @ApiImplicitParam(name = "proId", value = "传入要删除的商品 ID ", required = true, paramType = "query", dataType = "Integer")
    @ApiOperation(value = "根据商品的 proId 删除单个商品信息")
    @PostMapping("/deleteProduct")
    public Result deleteProduct(@RequestParam("proId") Integer proId){
        Boolean deleted = productInfoService.deleteProduct(proId);
        if (!deleted) {
            return new Result(ResultCode.FALSE);
        }
        return new Result(ResultCode.SUCCESS);
    }

    @ApiOperation(value = "根据商品的 proId 修改单个商品的信息")
    @ApiImplicitParam(name = "productInfo", value = "传入 ProductInfo 实体类", required = true, paramType = "body", dataTypeClass = ProductInfo.class)
    @PostMapping("/updateProduct")
    public Result updateProduct(@RequestBody ProductInfo productInfo){
        Boolean updated = productInfoService.updateProduct(productInfo);
        if (!updated) {
            return new Result(ResultCode.FALSE);
        }
        return new Result(ResultCode.SUCCESS);
    }
}
